/*************************************************************************
	> File Name: 160.相交链表.c
	> Author: Maureen 
	> Mail: Maureen@qq.com 
	> Created Time: 四  9/ 2 17:21:37 2021
 ************************************************************************/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
//双指针
//pA到达A链表的末尾后，指向B链表的头部；pB到达B链表的末尾后，指向A链表的头部
//若二者有相交节点，则最终pA = pB; 否则，最终pA和pB会以pA = pB = NULL 结束循环
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    if (headA == NULL || headB == NULL) return NULL;
    struct ListNode *pA = headA, *pB = headB;
    while (pA != pB) {
        pA = pA == NULL ? headB : pA->next;
        pB = pB == NULL ? headA : pB->next;
    } 
    return pA;
}
